﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.Linq;
using System.Xml;
using NetJsWire.Widgets.Core;
using Newtonsoft.Json;

namespace NetJsWire.Widgets.Renderers.Core
{
    /// <summary>
    ///   A simple basic class to enable render of non widget coponents like <see cref = "NetJsWire.Widgets.Data.Store" />.
    ///   <b><see cref = "BasicComponent" /> only renders construction properties, the derived class is responsible for everything else.</b>
    /// </summary>
    public abstract class BasicComponent : Element
    {
        /// <summary>
        ///   Default contructor.
        /// </summary>
        /// <param name = "baseObject">The component that this renderer is attached to.</param>
        public BasicComponent( object baseObject )
            : base( baseObject )
        {
        }

        public override void Render()
        {
            if ( !Rendered )
            {
                XmlNode optionsNode = ControlNode[ "options" ];

                SetConstructionProperty( "id", InternalID );
                SetAttribute( ControlNode, "name", InternalID );

                SetAttribute( ControlNode, "type", ClassName );
                SetAttribute( ControlNode, "render", true );

                foreach ( var cProp in ConstructionProperties )
                {
                    if ( !ReservedWords.Contains( cProp.Key ) )
                    {
                        SetAttribute( optionsNode, cProp.Key, JsonConvert.ToString( cProp.Value ) );
                    }
                    else
                    {
                        SetAttribute( optionsNode, cProp.Key, cProp.Value );
                    }
                }
                ConstructionProperties.Clear();
                Rendered = true;
            }
        }
    }
}